/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * assign2
 * Copyright (C)  Michael Morckos 2008 <mikey.morckos@gmail.com>
 * 
 * assign2 is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * assign2 is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _ASSIGN2_H_
#define _ASSIGN2_H_

#include  "plotter.cc"
#include  "intensity.h"

#include <gtkmm.h>
#include <gtkmm/button.h>
#include <gtkmm/main.h>

#include <opencv/cv.h>
#include <opencv/highgui.h>

#include <glib.h>
#include <stdlib.h>
#include <iostream>

using namespace Gtk;
using namespace std;

class Img_Editor : public Window {

    public:
	    Img_Editor();
        virtual ~Img_Editor();
			
        void loadImage();
		void displayImage();
		
		/* FEATURE EXTRACTIONG FOR RGB & HSV */
		bool extract(int, int);
		
		/* EQUALIZATION FOR THE V CHANNEL */
		void equalize();
		
		void convert();
		
		/* CONVERSION FROM RGB --> HSV */
		void convert_RGB_HSV();
		CvScalar convert_RGB_HSV_helper(CvScalar);
		
		/* CONVERSION FROM HSV --> RGB */
		void convert_HSV_RGB();
		CvScalar convert_HSV_RGB_helper(CvScalar);
		
		inline double max (double R, double G, double B) {
			
			double arr [3];
			arr[0] = R; arr[1] = G; arr[2] = B;
			double max = arr[0];
			
			for(int i = 0; i < 3; i++) {
				for(int j = 0; j < 3; j++) {
					if(max < arr[j]) {
					    max = arr[i];
						break;
					}	
				}	
			}
			
			return max;
		} 
		
		inline double min (double R, double G, double B) {
			
			double arr [3];
			arr[0] = R; arr[1] = G; arr[2] = B;
			double min = arr[0];
			
			for(int i = 0; i < 3; i++) {
				for(int j = 0; j < 3; j++) {
					if(min > arr[j]) {
					    min = arr[i];
						break;
					}	
				}	
			}
			
			return min;
		} 
		
		/* GRAPH PLOTTING FOR RGB AND V CHANNEL */
		void plot();
		void plotR();
		void plotG();
		void plotB();
		
		/* FEATURE EXTRACTION FOR GRAYSCALE IMAGES */
		bool extract_gray(int);
		
		/* GRAPH PLOTTING FOR RGB GRAYSCALE IMAGES */
		void plotSingle();
		
		/* CONTRAST STRETCHING */
		void stretch();
	
	protected:
	    virtual bool on_my_frame_event(GdkEvent*);
    
	private:
	    /* GTK */
		VBox main_vbox_1, main_vbox_2; 
	    VBox frame1_vbox_1_1, frame1_vbox_1_2;
	    VBox frame2_vbox;	
	  
		HBox main_hbox;
	    HBox frame1_hbox, hbox, hbox2;
	    HBox frame2_hbox_2_1, frame2_hbox_2_2;
	
	    RadioButton radio_1_1, radio_1_2, radio_1_3, radio_1_4;	
		
	    Frame frame1, frame2;
		
        Button load_button;
	    Button button_1_1, button_1_2, button_1_3, button_1_4, button_1_5, button_1_6;
	    Button button_2_1, button_2_2;
		
	//  SpinButton entry1, entry2;
	//	Adjustment aj_1, aj_2;
		
		Entry entry1, entry2;
		
	    Label label1, label2;
		
		/* openCV */
		const char* file_name;
		IplImage *img1, *img2, *img3;
		
		/* IMG DATA */
		Intensity* R;
		Intensity* G;
		Intensity* B;
		
		Intensity* H;
		Intensity* S;
		Intensity* V;
		
		Intensity* gray;
		
		bool toggleRGB, toggleStretch;
};

#endif // _ASSIGN2_H_

